package com.hh.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Author: huanghuai
 * @Date: 2020/7/28
 */
@Component
@Slf4j
public class Springboot拦截器过滤器配置等 implements WebMvcConfigurer {


    // 拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new DefaultInterceptor());
    }

    // 跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }


    // 过滤器
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new DefaultFilter());
        bean.addUrlPatterns("/*");
        return bean;
    }






    // message converter
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MsgC());
    }

    public static class MsgC implements HttpMessageConverter{

        @Override
        public boolean canRead(Class clazz, MediaType mediaType) {
            return false;
        }

        @Override
        public boolean canWrite(Class clazz, MediaType mediaType) {
            boolean b = true;
            return b;
        }

        @Override
        public List<MediaType> getSupportedMediaTypes() {
            return Arrays.asList(new MediaType("xml","bbb"));
        }

        @Override
        public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
            return null;
        }

        @Override
        public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
            outputMessage.getBody().write(new String("11111").getBytes());
        }
    }


    public static class DefaultFilter implements Filter {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {

        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest req = (HttpServletRequest) request;
            log.debug("filter：{} {} remote:{}", req.getMethod(), req.getRequestURL(), req.getRemoteAddr());
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {

        }
    }
    public static class DefaultInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            log.debug("preHandle：{} {} remote:{}", request.getMethod(), request.getRequestURL(), request.getRemoteAddr());
            return true;
        }
    }
}
